package com.demo.java.alg;

import org.junit.Assert;

public class MultipleChoiceTest {

    /**
     * 多选枚举
     */
    public enum MultipleChoiceEnum
    {
        A, B, C, D, E, F, G, H, I, J;
        private final int mask;
        private  MultipleChoiceEnum()
        {
            mask = 1 << ordinal();
            System.out.println("name: " + this.toString() + ", ordinal: " + ordinal() + ", mask: " + this.mask);
        }

        public final int getMask()
        {
            return mask;
        }

        /**
         * 判断是否已经选择
         *
         * @param multipleChoices
         * @param multipleChoiceEnum
         * @return
         */
        public static boolean isChoosed(int multipleChoices, MultipleChoiceEnum multipleChoiceEnum)
        {
            return 0 != (multipleChoices & multipleChoiceEnum.getMask());
        }
    }
    public static void main(String[] args) {
        int multipleChoices = 0;
        //选择了A
        multipleChoices |= MultipleChoiceEnum.A.getMask();
        System.out.println(multipleChoices);
        //选择了B
        multipleChoices |= MultipleChoiceEnum.B.getMask();
        System.out.println(multipleChoices);
        //选择C
        multipleChoices |= MultipleChoiceEnum.C.getMask();
        System.out.println(multipleChoices);

        multipleChoices |= MultipleChoiceEnum.D.getMask();
        System.out.println(multipleChoices);

        multipleChoices |= MultipleChoiceEnum.E.getMask();
        System.out.println(multipleChoices);

        multipleChoices |= MultipleChoiceEnum.F.getMask();
        System.out.println(multipleChoices);
        //突然看同桌不爽,把已选的C去掉
        multipleChoices &= ~MultipleChoiceEnum.C.getMask();
        System.out.println(multipleChoices);


        Assert.assertEquals(true, MultipleChoiceEnum.isChoosed(multipleChoices, MultipleChoiceEnum.A));
        Assert.assertEquals(true, MultipleChoiceEnum.isChoosed(multipleChoices, MultipleChoiceEnum.B));
        Assert.assertEquals(false, MultipleChoiceEnum.isChoosed(multipleChoices, MultipleChoiceEnum.C));
        Assert.assertEquals(false, MultipleChoiceEnum.isChoosed(multipleChoices, MultipleChoiceEnum.D));
    }
}
